package com.hinacom.pix.entity;

import org.apache.commons.lang.NullArgumentException;
import org.apache.commons.lang.StringUtils;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by fyu on 2016/9/23.
 */
public class PhoneNumber {
    private PhoneType type = null;
    private String countryCode = null;
    private String areaCode = null;
    private String number = null;
    private String extension = null;
    private String note = null;
    private String email = null;

    public PhoneNumber() {
    }

    public PhoneNumber(PhoneType type) {
        this.type = type;
    }

    public PhoneNumber(PhoneType type, String areaCode, String number) {
        this.type = type;
        this.areaCode = areaCode;
        this.number = number;
    }

    public String getAreaCode() {
        return this.areaCode;
    }

    public void setAreaCode(String areaCode) {
        String pattern = "(\\+|\\-)?\\d*\\.?\\d*";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(areaCode);
        if (m.find() == false) {
            throw new IllegalArgumentException(" The area code must be in the '010'");
        }

        this.areaCode = areaCode;
    }

    public String getCountryCode() {
        return this.countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public String getExtension() {
        return this.extension;
    }

    public void setExtension(String extension) {
        String pattern = "(\\+|\\-)?\\d*\\.?\\d*";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(extension);
        if (m.find() == false) {
            throw new IllegalArgumentException(" The extension must be in the 'X12345'");
        }

        this.extension = extension;
    }

    public String getNote() {
        return this.note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getNumber() {
        return this.number;
    }

    public void setNumber(String number) {
        String pattern = "(\\+|\\-)?\\d*\\.?\\d*";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(number);
        if (m.find() == false) {
            throw new IllegalArgumentException("The phone number format excption. The phone number must be in the '999-9999'");
        }

        this.number = number;
    }

    public PhoneType getType() {
        return this.type;
    }

    public void setType(PhoneType type) {
        this.type = type;
    }

    public boolean isEmpty() {
        return this.number == null && this.extension == null;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }






    public String getFullNumber() {
        String areaCode = "";
        if (StringUtils.isNotEmpty(this.getAreaCode())) {
            areaCode = String.format("(%s)", this.getAreaCode());
        }

        String number = "";
        if (StringUtils.isNotEmpty(this.getNumber())) {
            number = String.format("%s-%s", this.getNumber().substring(0,3), this.getNumber().substring(3));
        }
        String extension = "";
        if (StringUtils.isNotEmpty(this.getExtension())) {
            extension = String.format("X%s", this.getExtension());
        }
        String fullNumber = String.format("%s%s%s", areaCode, number, extension);
        return fullNumber;
    }
}

